/**
 A PanelWizard is a chain of Panels that interact with each other as a wizard.
 Each Panel has a next button that leads to some action and then the moving on to the next panel.
*/
PanelsWizard = function(varName,arrayOfPanels)
{
	this.varName = varName;
	// should contain an array of Panel objects
	this.arrayOfPanels = arrayOfPanels;
	
	this.activatePanel = function(panelId)
	{
		var panelToShow=null;
		for(var key in this.arrayOfPanels)
		{
			var currentPanel = this.arrayOfPanels[key];
			currentPanel.hide();
			if(panelId==currentPanel.panelId){
				panelToShow = currentPanel;
			}
		}
		if(panelToShow) panelToShow.show();	
	}

	this.getPanel = function(panelId)
	{
		var ret=null;
		for(var key in this.arrayOfPanels)
		{
			var currentPanel = this.arrayOfPanels[key];
			if(panelId==currentPanel.panelId){
				ret = currentPanel;
			}
		}
		if(ret) return ret;	
	}
	
	this.sendPanelRequest = function(panelId, evalAfterResponse)
	{
		var panel = this.getPanel(panelId);
		var callback = function(json){
			eval("var onClickFunction = function(e){"+this.varName+".activatePanel('"+nextPanel.panelId+"')}");
			onClickFunction();
		}
		panel.sendRequest(function(){eval(evalAfterResponse)});
	}
	/** startWizard simply activates the first panel in the wizard. */
	this.startWizard = function(){
		this.activatePanel(arrayOfPanels[0].panelId);
	}
	/* Attach a next event to each Panel (except the last) if it has a NextButton: */
	this.activateNextEvents = function(){
		for(var key in this.arrayOfPanels)
		{
			var currentPanel = this.arrayOfPanels[key];
			var nextPanel = this.arrayOfPanels[parseInt(key)+1];
			if(nextPanel && currentPanel.nextButton){
				//eval("var onClickFunction = function(e){"+this.varName+".activatePanel('"+nextPanel.panelId+"')}");
				// call the panel's sendRequest merhod:
				eval("var onClickFunction = function(e){"+this.varName+".sendPanelRequest(\""+currentPanel.panelId+"\",\""+this.varName+".activatePanel('"+nextPanel.panelId+"')\")}");
				currentPanel.nextButton.onclick = onClickFunction;
				
			}
		}
	}
	
	this.activateNextEvents();


}